package com.yuexunit.account.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import com.yuexunit.account.action.v2.model.AccountAlias;
import com.yuexunit.account.action.v2.model.AccountNameType;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.MatcherUtil;


/**
 * 帐号登录名 
 * @author wangjinshui
 */
@Entity
@Table(name="AC_ACCOUNT_NAME")
public class AccountNameEntity {
	@Id
	@GeneratedValue
	@Column(name="ACCOUNT_NAME_ID")
	private Long accountNameId;
	
	@Column(name="ACCOUNT_ID")
	private Long accountId;

	@Column(name="ACCOUNT_NAME")
	private String accountName;

	@Column(name="ACCOUNT_TYPE")
	@Enumerated(EnumType.ORDINAL)
	private AccountNameType type;

	@Column(name="CREATE_DT")
	private Date createDate;

	public Long getAccountNameId() {
		return accountNameId;
	}

	public void setAccountNameId(Long accountNameId) {
		this.accountNameId = accountNameId;
	}

	public Long getAccountId() {
		return accountId;
	}

	public void setAccountId(Long accountId) {
		this.accountId = accountId;
	}

	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public AccountNameType getType() {
		return type;
	}

	public void setType(AccountNameType type) {
		this.type = type;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	public void initCreateDate() {
		setCreateDate(DateUtil.now());
	}
	
	
	public void resetType() {
		if (MatcherUtil.isMobile(accountName)) {
            setType(AccountNameType.MOBILE);
		} else if (MatcherUtil.isEmail(accountName)) {
			setType(AccountNameType.EMAIL);
		}
	}
	
	
	public AccountAlias toAccountAlias() {
		AccountAlias result = new AccountAlias();
		
		result.setAccountId(accountId);
		result.setAccountName(accountName);
		result.setType(type);
		
		return result;
	}
	
	
	@Override
	public String toString() {
		StringBuffer result = new StringBuffer();
		
		result.append(getClass().getName())
			.append(": accountNameId=").append(accountNameId)
			.append(", accountId=").append(accountId)
			.append(", accountName='").append(accountName).append("'")
			.append(", type=").append(type)
			.append(", createDate=").append(createDate)
			;
		
		return result.toString();
	}
}
